package org.vergil.messagelistener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import org.vergil.websocket.MyWebSocketHandler;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 * Created by vergil on 5/1/17.
 */
@Component
public class UserMessageListener {

    private Logger log = LoggerFactory.getLogger(UserMessageListener.class);
    @Autowired
    private MyWebSocketHandler myWebSocketHandler;

    @JmsListener(destination = "devTest")
    public void onMessage(Message message) {
        //这里我们知道生产者发送的就是一个纯文本消息，所以这里可以直接进行强制转换
        TextMessage textMsg = (TextMessage) message;
        try {
            myWebSocketHandler.sendMessageToUsers(
                    new org.springframework.web.socket.TextMessage(textMsg.getText()));
            log.info("消息内容是：{} ", textMsg.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}
